package com.example.springboot.controller.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.springboot.common.Constants;
import com.example.springboot.common.Result;
import com.example.springboot.common.RoleEnum;
import com.example.springboot.config.interceptor.AuthAccess;
import com.example.springboot.controller.dto.UserDTO;
import com.example.springboot.controller.dto.UserPasswordDTO;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 登录注册用户表 API控制器 - 不需要token验证的API
 * </p>
 *
 * @author
 */
@RestController
@RequestMapping("api/user")
public class UserApi {
    @Resource
    private IUserService iUserService;



    @PostMapping("/login")
    public Result login(@RequestBody UserDTO userDTO) {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        UserDTO dto = iUserService.login(userDTO);
        return Result.success(dto);
    }



    // 新增或者更新
    @PostMapping("/register")
    public Result save(@RequestBody User user) {
        String username = user.getUsername();
        if (StrUtil.isBlank(username)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        if (StrUtil.isBlank(user.getNickname())) {
            user.setNickname(username);
        }
        User one = iUserService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));
        if (one == null) {
            if (user.getId() != null) {
                user.setPassword(null);
            } else {
                if (user.getPassword() == null) {
                    user.setPassword("123");
                }
            }
            user.setRole(RoleEnum.ROLE_USER.toString());
//            save(user);  // 把 copy完之后的用户对象存储到数据库
            return Result.success(iUserService.saveOrUpdate(user));
        }else {
            throw new ServiceException(Constants.CODE_600, "用户已存在");
        }

    }

    //重置密码
    @AuthAccess
    @PutMapping("/reset")
    public Result reset(@RequestBody UserPasswordDTO userPasswordDTO) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userPasswordDTO.getUsername());
        queryWrapper.eq("phone", userPasswordDTO.getPhone());
        List<User> list = iUserService.list(queryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            User user = list.get(0);
            user.setPassword("123456");
            iUserService.updateById(user);
        }else{
            throw new ServiceException("400","用户名或者手机号不存在");
        }
        return Result.success();
    }

    /**
     * 修改密码
     *
     * @param userPasswordDTO
     * @return
     */
    @PostMapping("/password")
    public Result password(@RequestBody UserPasswordDTO userPasswordDTO) {
        iUserService.updatePassword(userPasswordDTO);
        return Result.success();
    }


    @GetMapping("/info/{userName}")
    public Result findByUsername(@PathVariable String userName) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userName);
        return Result.success(iUserService.getOne(queryWrapper));
    }

    @PostMapping("/update")
    public Result updateByUsername(@RequestBody User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        return Result.success(iUserService.update(user,queryWrapper));
    }

}
